/*
 * Created by JFormDesigner on Wed Aug 03 20:02:36 CST 2022
 */

package client.qxMange;

import client.util.ClientConn;
import command.Command;
import command.qxComm.BackCommand;
import command.qxComm.FindCommand;
import command.qxComm.GrantCommand;
import empty.power.Power;
import model.qxModel.PowerModel;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Arrays;
import java.util.List;

/**
 * @author Brainrain
 */
public class PFramePanel extends JPanel {

    PowerModel pm = new PowerModel();
    ClientConn cc = new ClientConn();

    public PFramePanel() {
        initComponents();
        if (ClientConn.socket == null) {
            try {
                cc.connect();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    private Power readPowerFrame() {
        Power p = new Power();
        if (textField1.getText() != null && !"".equals(textField1.getText())) {
            p.setId(Integer.parseInt(textField1.getText()));
        }
        p.setUser_id(Integer.parseInt(textField2.getText()));
        p.setFile_id(Integer.parseInt(textField3.getText()));

        if (cB1.getSelectedItem() == "只读")
        {p.setFile_qx("10");}
        else if (cB1.getSelectedItem() == "只写")
        {p.setFile_qx("01");}
        else if (cB1.getSelectedItem() == "读写")
        {p.setFile_qx("11");}
        else if (cB1.getSelectedItem() == "无")
        {p.setFile_qx("00");}

        return p;
    }

    private void grant(ActionEvent e) {
        Power p = readPowerFrame();
        Command gcom = new GrantCommand();
        gcom.o = p;
        try {
            cc.send(gcom);
            gcom = (Command) cc.receive();
            if (gcom.e != null) {
                JOptionPane.showMessageDialog(null, "服务器错误！" + gcom.e.getMessage());
            } else {
                JOptionPane.showMessageDialog(null, "成功添加" + gcom.r + "条");
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
    }

    private void back(ActionEvent e) {
        Power p = new Power();
        p.setId(Integer.parseInt(textField1.getText()));
        Command bcom = new BackCommand();
        bcom.o = p;
        try {
            cc.send(bcom);
            bcom = (Command) cc.receive();
            if (bcom.e != null) {
                JOptionPane.showMessageDialog(null, "服务器错误！" + bcom.e.getMessage());
            } else {
                JOptionPane.showMessageDialog(null, "成功添加" + bcom.r + "条");
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
    }

    private void find(ActionEvent e) throws Exception{
        Power p1 = new Power();
        Command fcom = new FindCommand();

        fcom.o = p1;
        cc.send(fcom);
        fcom = (Command) cc.receive();


        if (fcom.e != null) {
            JOptionPane.showMessageDialog(null, "服务器错误！" + fcom.e.getMessage());
        } else {
            List<Object[]> result = (List<Object[]>) fcom.r;

            DefaultTableModel dtm = new DefaultTableModel();
            table1.setModel(dtm);
            Object[][] data = new Object[result.size()][];//.size集合中元素的个数，data有.size行
            for (int i = 0; i < result.size(); i++) {
                data[i] = Arrays.stream(result.get(i)).toArray();
            }
            String header[] = {"ID","用户id","文件id","文件权限"};
            dtm.setDataVector(data, header);
        }
    }



    private void findUid(ActionEvent e) throws Exception {
        Power p1 = new Power();
        if (textField5.getText() != null && !"".equals(textField5.getText())) {
            p1.setUser_id(Integer.parseInt(textField5.getText()));
        }

        Command cfc = new FindCommand();
        cfc.o = p1;
        cc.send(cfc);
        cfc =  (Command) cc.receive();

        if(cfc.e!= null){
            JOptionPane.showMessageDialog(null,"服务器错误！"+cfc.e.getMessage());
        }
        else{
            List<Object[]> result = (List<Object[]>)cfc.r;
            DefaultTableModel dtm = new DefaultTableModel();
            table1.setModel(dtm);
            Object[][] data = new Object[result.size()][];
            for (int i = 0;i<result.size();i++){
                data[i] = Arrays.stream(result.get(i)).toArray();
            }
            String header[] = {"ID","用户id","文件id","文件权限"};
            dtm.setDataVector(data,header);
        }
    }

    private void findFid(ActionEvent e) throws Exception {
        Power p1 = new Power();
        if (textField7.getText() != null && !"".equals(textField7.getText())) {
            p1.setFile_id(Integer.parseInt(textField7.getText()));
        }

        Command cfc = new FindCommand();
        cfc.o = p1;
        cc.send(cfc);
        cfc =  (Command) cc.receive();



        if(cfc.e!= null){
            JOptionPane.showMessageDialog(null,"服务器错误！"+cfc.e.getMessage());
        }
        else{
            List<Object[]> result = (List<Object[]>)cfc.r;
            DefaultTableModel dtm = new DefaultTableModel();
            table1.setModel(dtm);
            Object[][] data = new Object[result.size()][];
            for (int i = 0;i<result.size();i++){
                data[i] = Arrays.stream(result.get(i)).toArray();
            }
            String header[] = {"ID","用户id","文件id","文件权限"};
            dtm.setDataVector(data,header);
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        tabbedPane1 = new JTabbedPane();
        panel2 = new JPanel();
        label6 = new JLabel();
        label8 = new JLabel();
        textField5 = new JTextField();
        textField7 = new JTextField();
        button3 = new JButton();
        button5 = new JButton();
        button6 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel1 = new JPanel();
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        textField2 = new JTextField();
        label4 = new JLabel();
        textField3 = new JTextField();
        cB1 = new JComboBox<>();
        button1 = new JButton();
        button2 = new JButton();

        //======== this ========
        setLayout(new BorderLayout());

        //======== tabbedPane1 ========
        {
            tabbedPane1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

            //======== panel2 ========
            {

                //---- label6 ----
                label6.setText("\u7528\u6237ID\uff1a");

                //---- label8 ----
                label8.setText("\u6587\u4ef6ID\uff1a");

                //---- button3 ----
                button3.setText("\u67e5\u8be2");
                button3.addActionListener(e -> {
                    try {
                        findUid(e);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                });

                //---- button5 ----
                button5.setText("\u67e5\u8be2");
                button5.addActionListener(e -> {
                    try {
                        findFid(e);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                });

                //---- button6 ----
                button6.setText("\u67e5\u8be2\u5168\u90e8");
                button6.addActionListener(e -> {
                    try {
                        find(e);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                });

                //======== scrollPane1 ========
                {
                    scrollPane1.setViewportView(table1);
                }

                GroupLayout panel2Layout = new GroupLayout(panel2);
                panel2.setLayout(panel2Layout);
                panel2Layout.setHorizontalGroup(
                    panel2Layout.createParallelGroup()
                        .addGroup(panel2Layout.createSequentialGroup()
                            .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(button6, GroupLayout.PREFERRED_SIZE, 154, GroupLayout.PREFERRED_SIZE)
                                .addGroup(panel2Layout.createParallelGroup()
                                    .addGroup(panel2Layout.createSequentialGroup()
                                        .addGap(49, 49, 49)
                                        .addGroup(panel2Layout.createParallelGroup()
                                            .addGroup(panel2Layout.createSequentialGroup()
                                                .addComponent(label6)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(textField5, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE))
                                            .addGroup(panel2Layout.createSequentialGroup()
                                                .addComponent(label8)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(textField7, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE)))
                                        .addGap(48, 48, 48)
                                        .addGroup(panel2Layout.createParallelGroup()
                                            .addComponent(button3)
                                            .addComponent(button5)))
                                    .addGroup(panel2Layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 731, GroupLayout.PREFERRED_SIZE))))
                            .addContainerGap(43, Short.MAX_VALUE))
                );
                panel2Layout.setVerticalGroup(
                    panel2Layout.createParallelGroup()
                        .addGroup(panel2Layout.createSequentialGroup()
                            .addGap(39, 39, 39)
                            .addGroup(panel2Layout.createParallelGroup()
                                .addComponent(label6)
                                .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                    .addComponent(textField5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(button3)))
                            .addGap(22, 22, 22)
                            .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label8)
                                .addComponent(textField7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(button5))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button6)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(39, 39, 39))
                );
            }
            tabbedPane1.addTab("\u6743\u9650\u67e5\u8be2", panel2);

            //======== panel1 ========
            {

                //---- label1 ----
                label1.setText("\u6743\u9650ID\uff1a");

                //---- label3 ----
                label3.setText("\u7528\u6237ID\uff1a");

                //---- label4 ----
                label4.setText("\u6587\u4ef6ID\uff1a");

                //---- cB1 ----
                cB1.setModel(new DefaultComboBoxModel<>(new String[] {
                    "\u53ea\u8bfb",
                    "\u53ea\u5199",
                    "\u8bfb\u5199",
                    "\u65e0"
                }));

                //---- button1 ----
                button1.setText("\u6388\u6743");
                button1.addActionListener(e -> grant(e));

                //---- button2 ----
                button2.setText("\u56de\u6536");
                button2.addActionListener(e -> back(e));

                GroupLayout panel1Layout = new GroupLayout(panel1);
                panel1.setLayout(panel1Layout);
                panel1Layout.setHorizontalGroup(
                    panel1Layout.createParallelGroup()
                        .addGroup(panel1Layout.createSequentialGroup()
                            .addGroup(panel1Layout.createParallelGroup()
                                .addGroup(panel1Layout.createSequentialGroup()
                                    .addGap(43, 43, 43)
                                    .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(label3)
                                        .addComponent(label4))
                                    .addGap(18, 18, 18)
                                    .addGroup(panel1Layout.createParallelGroup()
                                        .addGroup(panel1Layout.createSequentialGroup()
                                            .addGap(117, 117, 117)
                                            .addComponent(label2))
                                        .addGroup(panel1Layout.createSequentialGroup()
                                            .addGroup(panel1Layout.createParallelGroup()
                                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
                                                .addComponent(textField3, GroupLayout.Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE))
                                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(cB1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                        .addComponent(button1, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)))
                                .addGroup(panel1Layout.createSequentialGroup()
                                    .addGap(44, 44, 44)
                                    .addComponent(label1)
                                    .addGroup(panel1Layout.createParallelGroup()
                                        .addGroup(panel1Layout.createSequentialGroup()
                                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 174, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(panel1Layout.createSequentialGroup()
                                            .addGap(14, 14, 14)
                                            .addComponent(button2, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)))))
                            .addContainerGap(418, Short.MAX_VALUE))
                );
                panel1Layout.setVerticalGroup(
                    panel1Layout.createParallelGroup()
                        .addGroup(panel1Layout.createSequentialGroup()
                            .addGap(31, 31, 31)
                            .addComponent(label2)
                            .addGap(33, 33, 33)
                            .addGroup(panel1Layout.createParallelGroup()
                                .addComponent(label3)
                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addGap(29, 29, 29)
                            .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(cB1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label4))
                            .addGap(31, 31, 31)
                            .addComponent(button1)
                            .addGap(60, 60, 60)
                            .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label1)
                                .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addGap(33, 33, 33)
                            .addComponent(button2)
                            .addContainerGap(200, Short.MAX_VALUE))
                );
            }
            tabbedPane1.addTab("\u6743\u9650\u8c03\u6574", panel1);
        }
        add(tabbedPane1, BorderLayout.CENTER);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JTabbedPane tabbedPane1;
    private JPanel panel2;
    private JLabel label6;
    private JLabel label8;
    private JTextField textField5;
    private JTextField textField7;
    private JButton button3;
    private JButton button5;
    private JButton button6;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel1;
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField2;
    private JLabel label4;
    private JTextField textField3;
    private JComboBox<String> cB1;
    private JButton button1;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

}
